home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Language/OS - Multiplatform Resource Library
/
LANGUAGE OS.iso
/
oper_sys
/
oasis
/
oasis1-1.lha
/
oasis-1.1
/
scan.c
< prev
next >
Wrap
C/C++ Source or Header
|
1992-05-01
|
10KB
|
244 lines
/*==========================================================================*
Oasis Alpha Version 1.1 (C) Copyright 1992 Fah-Chun Cheong
Revised: 5/1/92 by: fcc@eecs.umich.edu and The University of Michigan
------------------------------------------------------------------------
Permission to use, copy, modify, distribute, sell and resell Oasis Alpha
software and its documentation for any purpose and without fee is hereby
granted, provided that the authorship be appropriately credited and
acknowledged, and that the above copyright notice appear in all copies
and both the copyright notice and this permission notice appear in
supporting documentation. The author makes no representations about the
suitability of this software for any purpose. It is provided "as is"
without express or implied warranty. Oasis Alpha is free, caveat emptor!
------------------------------------------------------------------------
To request Oasis Alpha source code: oasis-alpha-request@eecs.umich.edu
To enroll in the mailing list: oasis-alpha-request@eecs.umich.edu
To send bug reports: oasis-alpha-bugs@eecs.umich.edu
To discuss openly all matters Oasis: oasis-alpha@eecs.umich.edu
*==========================================================================*/
#include <sys/types.h>
#include "gener.h"
#include "gen.h"
#define E_LEX_CHAR "Missing right single quote (') in character '%c'.\n"
#define E_LEX_STRING "Missing right double quote (\") in string \"%s\".\n"
#define E_LEX_TOKEN "Bad token (ascii = %d) discarded.\n"
#define err1(s,a) fprintf(stderr, s, a)
extern u_long getaddr();
static char *ptr;
static char *str;
static int ascii[] = {
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 1, 19, 15, 11, 1, 13, 18, 1, 1, 1, 1, 1, 1, 2, 1,
17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 4, 1, 7, 6, 8, 3,
16, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 1, 0, 1, 0, 10,
14, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 1, 5, 1, 12, 0
};
void scan_escape()
{
switch (*++ptr) {
case 'n': *ptr = '\n'; break;
case 't': *ptr = '\t'; break;
case 'b': *ptr = '\b'; break;
case 'r': *ptr = '\r'; break;
case 'f': *ptr = '\f'; break;
default: if (isdigit(*ptr)) {
char *ptr0 = ptr;
do ptr++;
while (isdigit(*ptr));
*--ptr = (char) atoi(ptr0);
}
}
}
int scan_token()
{
AGAIN: while (isspace(*ptr))
ptr++;
switch (ascii[*ptr]) {
case 1: return *ptr++;
case 2: if (isspace(ptr[-1]) || isspace(ptr[1]))
return *ptr++;
ptr++;
return DOT;
case 3: switch (*++ptr) {
case '-': ptr++; return DO;
default: return '?';
}
case 4: switch (*++ptr) {
case ':': ptr++; return ISA;
case '-': ptr++; return IF;
case 'c': ptr++; return C;
case 'i': ptr++; return I;
case 'f': ptr++; return F;
case 'o': ptr++; return O;
case 'a': ptr++; return A;
case 'y': ptr++; return Y;
case 'l': ptr++; return L;
case '$': zzlval.i = atoi(++ptr);
while (isdigit(*ptr))
ptr++;
return G;
default: return ':';
}
case 5: switch (*++ptr) {
case '-': ptr++; return THEN;
default: return '|';
}
case 6: switch (*++ptr) {
case '>': ptr++; return RETURN;
case '=': ptr++; return EQ;
default: return '=';
}
case 7: switch (*++ptr) {
case '>': ptr++; return NE;
case '=': ptr++; return LE;
default: return LT;
}
case 8: switch (*++ptr) {
case '=': ptr++; return GE;
default: return GT;
}
case 9: zzlval.s = str;
do *str++ = *ptr++;
while (isalnum(*ptr) || *ptr == '_');
*str++ = '\0';
return ID;
case 10: zzlval.s = str;
do *str++ = *ptr++;
while (isalnum(*ptr) || *ptr == '_');
*str++ = '\0';
return strcmp(zzlval.s, "_") ? CID : '_';
case 11: zzlval.s = str;
for (ptr++; isalpha(*ptr);)
*str++ = *ptr++;
*str++ = '\0';
return !strcmp(zzlval.s, "nil") ? NIL
: !strcmp(zzlval.s, "true") ? TRUE
: !strcmp(zzlval.s, "fail") ? FAIL
: !strcmp(zzlval.s, "wait") ? WAIT
: !strcmp(zzlval.s, "post") ? POST : '$';
case 12: zzlval.i = atoi(++ptr);
while (isdigit(*ptr)) ptr++;
return VID;
case 13: zzlval.i = atoi(++ptr);
while (isdigit(*ptr)) ptr++;
return MID;
case 14: zzlval.i = atoi(++ptr);
while (isdigit(*ptr)) ptr++;
return GID;
case 15: zzlval.i = atoi(++ptr);
ptr++;
while (isdigit(*ptr)) ptr++;
return LID;
case 16: zzlval.s = str;
while (isspace(*++ptr)) ;
if (isdigit(*ptr))
do do *str++ = *ptr++;
while (isdigit(*ptr));
while (*ptr == '.' && isdigit(ptr[1]));
else if (isalpha(*ptr))
do do *str++ = *ptr++;
while (isalnum(*ptr));
while (*ptr == '.' && isalpha(ptr[1]));
*str++ = '\0';
zzlval.i = getaddr(zzlval.s);
return ATINET;
case 17: zzlval.s = ptr;
while (isdigit(*++ptr)) ;
if (*ptr == '.' && isdigit(ptr[1])) {
while (isdigit(*++ptr)) ;
if (*ptr == 'e' || *ptr == 'E') {
EXPO: if (ptr[1] == '-') ptr++;
while (isdigit(*++ptr)) ;
}
zzlval.f = atof(zzlval.s);
return FLOAT;
}
if (*ptr == 'e' || *ptr == 'E')
goto EXPO;
zzlval.i = atoi(zzlval.s);
return INTEGER;
case 18: if (ptr[-1] == '-' || ptr[-1] == '/' || ptr[-1] == '%')
return *ptr++;
if (*++ptr == '\\')
scan_escape();
zzlval.c = *ptr++;
if (*ptr++ != '\'')
err1(E_LEX_CHAR, ptr[-2]);
return CHARACTER;
case 19: zzlval.s = str;
ptr++;
while (*ptr != '"' && *ptr != '\n') {
if (*ptr == '\\')
scan_escape();
*str++ = *ptr++;
}
*str++ = '\0';
if (*ptr++ != '"')
err1(E_LEX_STRING, zzlval.s);
return STRING;
default: err1(E_LEX_TOKEN, *ptr++);
goto AGAIN;
}
}
void zzerror()
{
extern int zzchar;
if (isprint(zzchar))
err1("Bad syntax '%c'.\n", zzchar);
else err1("Bad syntax (token code = %d).\n", zzchar);
}
int zzlex()
{
int token = scan_token();
#ifdef SCAN_BUG
fprintf(stderr, "token code = %3d %c\n", token, isprint(token) ? token : ' ');
#endif SCAN_BUG
return token;
}
void init_scan(buf)
char *buf;
{
str = buf;
ptr = buf;
}